package com.changfen.service.impl;

import com.changfen.dao.AccountDao;
import com.changfen.dao.JpaBaseDao;
import com.changfen.domain.Account;
import com.changfen.dto.OrderDto;
import com.changfen.feign.OrderClient;
import com.changfen.service.AccountService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author changfeng.chen@things-matrix.com
 * @date 2022/9/21 16:53
 **/

@Slf4j
@Service
public class AccountServiceImpl extends JpaBaseServiceImpl<Account,Long> implements AccountService {
    @Resource
    private AccountDao accountDao;

    @Resource
    private OrderClient orderClient;

    @Override
    protected JpaBaseDao<Account, Long> getJpaBaseDao() {
        return accountDao;
    }


    @Override
    @GlobalTransactional(timeoutMills = 30000,name = "gts-seata-example")
    public void addAccountAndOrder(Long userId){
        Account account = getOne(userId);

        account.setMoney(account.getMoney().add(BigDecimal.TEN));

        accountDao.save(account);

        orderClient.addOrder(
                OrderDto.builder()
                        .userId(String.valueOf(userId))
                        .commodityCode("c000001")
                        .count(10L)
                        .money(100L)
                        .build()
        );

    }
}
